<?php
/**
 * View
 * 
 * @category	Application
 * @package		dayscript.application
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Juan Carlos Orrego $
 * @modified	$Date: 2009-10-21 $
 */

namespace dayscript\application;

/**
 * View
 * 
 * @category	Application
 * @package		dayscript.application
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\event\EventDispatcher, \dayscript\log\LogManager, \dayscript\Map, \dayscript\exception\Exception
 * @since		1.0
 * @modifiedby	$LastChangedBy: Juan Carlos Orrego $
 * @modified	$Date: 2009-10-21 $
 */
class View extends \dayscript\Map	{
	/**
	 * View identifier
	 * @var string $name
	 */
	protected $name = null;
	/**
	 * View file
	 * @var string $file
	 */
	protected $file = null;
	/**
	 * View's Module object
	 * @var \dayscript\application\Module $module
	 */
	protected $module = null;
	/**
	 * Aggregates an instance of the View class.
	 * @param string $name View name
	 * @param \dayscript\application\Module $module Module object
	 * @param string $file render file location
	 */
	public function __construct( $name, \dayscript\application\Module $module, $file = null )	{
		parent::__construct( );
		$this->name = $name;
		$this->file = $file;
		$this->module = $module;
	}

	/*
	 * Getters / Setters
	 */
	public function getName( )	{
		return $this->name;
	}
	public function getModule( )	{
		return $this->module;
	}
	public function getFile( )	{
		return $this->file;
	}
	
	public function render( $file = null )	{
		$file = \str_replace( '.', DIRECTORY_SEPARATOR, ( $file != null ? $file : ( $this->file ? $this->file : $this->name ) ) );
		
		if( !$file ){
			throw new \dayscript\exception\Exception( 'View renderer error. No valid file.', \E_USER_ERROR );		
		}
		
		$file .= ( \preg_match( '/\\.php$/i', $file ) ? '' : '.php' );
		
		if( !\file_exists( $this->module->getViewsDir( ) . $file ) ){
			// This has been modified to allow normal execution
			// in absence of view file. NOTICE logged.
			// throw new \dayscript\exception\Exception( 'View renderer error. File not found: ' . $this->module->getName( ) . '/views/' . $file, \E_USER_ERROR );
			
			$this->module->log('View renderer notice. File not found: ' . $this->module->getName( ) . '/views/' . $file,\dayscript\log\LogManager::LOG_NOTICE);
			
			// Here should be the code for handling when an incorrect URL is reached.
		} else {
			require( $this->module->getViewsDir( ) . $file );
		}
		
	}
	
}
